package com.java2007.hotel.controller.backend;

import com.alibaba.druid.util.StringUtils;
import com.java2007.hotel.constant.ServletConstant;
import com.java2007.hotel.controller.Controller;
import com.java2007.hotel.pojo.Order;
import com.java2007.hotel.pojo.OrderDetail;
import com.java2007.hotel.pojo.User;
import com.java2007.hotel.service.OrderDetailService;
import com.java2007.hotel.service.OrderService;
import com.java2007.hotel.service.impl.OrderDetailServiceImpl;
import com.java2007.hotel.service.impl.OrderServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * @author Reckeys
 * @version V1.0
 * @date 2021/01/29
 **/
@WebServlet("/orderbackend")
public class OrderBackendController extends Controller {
    private OrderService orderService = new OrderServiceImpl();
    private OrderDetailService orderDetailService = new OrderDetailServiceImpl();

    /**
     * 查询所有订单
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String findAllOrder(HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<Order> orderList = orderService.findAllOrder();
        request.setAttribute("orderList",orderList);
        return "forward:/backend/detail/order/order-list.jsp";

    }

    /**
     * 修改支付状态
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String updateOrderStatus(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取订单id和订单支付状态

        String orderIdStr = request.getParameter("orderId");
        String orderPayStatus = request.getParameter("orderPayStatus");
        if (orderIdStr == null || StringUtils.isEmpty(orderIdStr)){
            return ServletConstant.jump("订单id错误","../bottom.jsp");
        }
        if ("1".equals(orderPayStatus)){
            return ServletConstant.jump("已结账，不要重复结账","orderbackend?method=findAllOrder");
        }

        Long orderId = Long.valueOf(orderIdStr);
        Order order = orderService.findOrder(orderId);
        order.setOrderId(orderId);
        order.setOrderPayStatus(1);
        order.setOrderUpdateTime(new Date());
        orderService.updateOrder(order);
        return ServletConstant.jump("支付成功","orderbackend?method=findAllOrder");
    }

    /**
     * 通过订单id查询订单详情
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String findOrderDetail(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String orderId = request.getParameter("orderId");
        if (orderId.isEmpty() || null == orderId){
            return ServletConstant.jump("订单ID异常","orderbackend?method=findAllOrder");
        }
        Long detailOrderId = Long.valueOf(orderId);
        List<OrderDetail> orderDetailList = orderDetailService.findOrderDetail(detailOrderId);
        request.setAttribute("orderDetailList",orderDetailList);
        return "forward:/backend/detail/order/order-detail.jsp";
    }



}